<?php
namespace app\index\validate;

use think\Validate;

class Withdraw extends Validate
{

    protected $rule = [
        'user_type' => 'checkUserType',
        'amount' => 'require|checkAmount',
        'bank' => 'require',
        'card' => 'require',
        'name' => 'require',
    ];

    protected $message = [
        'amount.require' => '请填写提现金额',
        'bank.require' => '请选择提现银行',
        'card.require' => '请填写银行卡号',
        'name.require' => '请填写真实姓名',
    ];

    /**
     * 验证用户类型
     * @param $value
     * @return bool|string
     */
    protected function checkUserType($value) {
        if (in_array($value, [0,1])) {
            return true;
        } else {
            return '参数错误：user_type';
        }
    }

    /**
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    protected function checkAmount($value, $rule, $data)
    {
        $min_withdraw_amount = getSystemParams('min_withdraw_amount');
        if ($value < $min_withdraw_amount) {
            return '提现金额必须大于或等于' . $min_withdraw_amount;
        } else {
            if ($data['user_type'] == 0) {
                if ($value > session('user.balance')) {
                    return '余额不足';
                }
            } else {
                if ($value > db('business')->where(['user_id'=>session('user.id')])->value('balance')) {
                    return '余额不足';
                }
            }
        }

        return true;
    }
}


